package uk.ac.ebi.uniprot.wikipedia.beanServices;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import uk.ac.ebi.uniprot.wikipedia.exceptions.ServiceException;

import java.util.Properties;

/**
 * Created with IntelliJ IDEA.
 * User: ljgarcia
 * Date: 04/07/12
 * Time: 13:00
 * To change this template use File | Settings | File Templates.
 */
public class SpringContextLoader {
    private static Logger logger = Logger.getLogger(SpringContextLoader.class);
    private AbstractApplicationContext applicationContext;
    private PropertyPlaceholderConfigurer propertyPlaceholderConfigurer;
    private static SpringContextLoader ourInstance = new SpringContextLoader();

    public static SpringContextLoader getInstance() {
        return ourInstance;
    }

    private SpringContextLoader() {
        try {
            applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");

            propertyPlaceholderConfigurer = new PropertyPlaceholderConfigurer();
            propertyPlaceholderConfigurer.setLocalOverride(true);
            //loading the cache and index locations
            propertyPlaceholderConfigurer.setLocation(new ClassPathResource("wikipedia_spring.properties"));
            applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);
            applicationContext.refresh();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error loading the application context for the wikipedia servers " + e.getMessage());
        }
    }

    public void setProperties(Properties props) {
        try {
            propertyPlaceholderConfigurer.setProperties(props);
            applicationContext.addBeanFactoryPostProcessor(propertyPlaceholderConfigurer);
            applicationContext.refresh();
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("Error loading the application context for the wikipedia servers " + e.getMessage());
        }
    }

    /**
     * Retruns a Spring bean
     *
     * @param beanName - Name of the Spring bean you want to retrieve
     * @return - a bean singleton in the form of an object object
     * @throws uk.ac.ebi.uniprot.wikipedia.exceptions.ServiceException - is thrown when the application context cant be initialized
     */
    public Object getBean(String beanName) throws ServiceException {
        if (this.applicationContext != null) {
            return applicationContext.getBean(beanName);
        } else {
            throw new ServiceException("Unable to localize the specified bean " + beanName);
        }
    }

    /*
    * Without spring
    public static DefaultListableBeanFactory getFactory() {
        ClassPathResource res = new ClassPathResource("applicationContext.xml");
        XmlBeanFactory factory = new XmlBeanFactory(res);
        return factory;
    }
    */
}
